<template>
	<view class="box">
		<!-- 选择会员 -->
		<view class="selectMember">
			<view class="member">
				<view @click="dialog = true">{{userName || '会员：'}}</view>
				<view style="position: relative;">{{userName?start_date:'汇总日期：'}}</view>
			</view>
			<view class="toSend" @click="creat">生成评估</view>
		</view>
		
		<view class="hasData" v-if="reData">
			<view v-for="(item,index) in allData" :key="index">
				<view class="summary" v-if="reData && reData[index] && reData[index].servId != 0">
					<image :src="'../../static/img/healthStatus/currentSummary/currentSummary'+(index+1)+'@2x.png'" class="topLeft"></image>
					<view class="top">
						<image src="@/static/img/quickly/healthStatus/number@2x.png"></image>
						<view>{{reData && reData[index] && reData[index].assessmentSn}}</view>
						<view>{{reData && reData[index] && reData[index].assessmentDate}}</view>
					</view>
					<view class="bottom">
						<view>{{userName}}</view>
						<view @click="detail(reData[index])">详情</view>
					</view>
				</view>
				<view class="noData" v-else>
					<image :src="'../../static/img/healthStatus/currentSummary/currentSummary'+(index+1)+'@2x.png'" class="topLeft"></image>
					无数据
				</view>
			</view>
		</view>
		<view class="metadata" v-else>
			<view class="case" v-for="(item,index) in allData" :key="index">
				<image src="@/static/img/healthStatus/currentSummary/metadata.png"></image>
				<view class="title">{{item}}</view>
				<view>无数据</view>
			</view>
		</view>
		<member :dialog.sync="dialog"  @hidedialog="hidedialog" @chooseUser="chooseUser"></member>
		<reportImg ref="reportImg" :info="info"></reportImg>
	</view>
</template>

<script>
	import member  from '@/components/selectMember/selectMember.vue';
	import reportImg  from './reportImg.vue';
	export default{
		components:{member,reportImg},
		data () {
			const currentDate = this.getDate({
				format: true
			})
			return {
				start:'',
				end:currentDate,
				start_date:currentDate,
				userName:'',
				dialog:false,
				allData:['会员基本信息','体检指标评估','生活方式评估','心理健康评估','健康素养评估','焦虑状态评估','抑郁状态评估'],
				reData: null,
				info:{}
			}
		},
		methods:{
			//关闭弹出框
			hidedialog(){
				this.dialog = false
			},
			//选择会员
			chooseUser(e){
				this.userId = e.userId;
				this.userName = e.userName;
				this.$api.getAppAssessSummaryInfo(e.userId,1).then((res)=>{
					if(res.data.returnCode == 200){
						this.reData = res.data.result
					}else{
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}
				})
			},
			// 日期
			bindDateChange(e) { 
				this.start_date = e.target.value;
			},
			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
			detail(item){
				if(item.type == 2){
					// 体检指标
					uni.navigateTo({url:`/pages/quickly/healthStatus/examined/toView?id=${item.servId}&userId=${this.userId}`})
				}else if(item.type == 3){
					// 生活方式
					uni.navigateTo({url:`/pages/quickly/healthStatus/lifeStyle/toView?id=${item.servId}&userId=${this.userId}`})
				}else if(item.type == 4){
					// 心理评估
					uni.navigateTo({url:`/pages/quickly/psychology/toView?id=${item.servId}&userId=${this.userId}`})
				}else if(item.type == 5){
					// 健康素养
					uni.navigateTo({url:`/pages/quickly/healthStatus/accomplishment/toView?id=${item.servId}&userId=${this.userId}`})
				}else if(item.type == 1){
					// 个人基本信息
					uni.navigateTo({url:`/pages/quickly/information/toView?id=${item.servId}`})
				}else if(item.type == 6){
					// 焦虑
					uni.navigateTo({url:`/pages/quickly/healthStatus/anxious/toView?id=${item.servId}&userId=${this.userId}`})
				}else if(item.type == 7){
					// 抑郁
					uni.navigateTo({url:`/pages/quickly/healthStatus/depressed/toView?id=${item.servId}&userId=${this.userId}`})
				}
			},
			creat(){
				let _this = this;
				if(!this.userId) {
					uni.showToast({
						title:'请选择会员',
						icon:'none'
					})
					return
				}
				if(!this.start_date) {
					uni.showToast({
						title:'请选择日期',
						icon:'none'
					})
					return
				}
				const data = this.reData.filter((a)=>{
					return a.assessmentSn != null
				})
				const defaultData = data.filter((a)=>{
					return a.title == '会员基本信息'
				})
				if(defaultData.length <= 0){
					uni.showToast({
						title:'请先完善会员基本信息。',
						icon:'none'
					})
					return
				}
				if(data.length <= 1){
					uni.showToast({
						title:'用户至少存在一份评估才可生成汇总。',
						icon:'none'
					})
					return
				}
				uni.pageScrollTo({
					scrollTop:0
				})
				this.$api.appAssessSummaryxzhzReport(this.userId,data).then((res)=>{
					if(res.data.returnCode == 200){
						this.info = res.data.result
						setTimeout(()=>{
							_this.$refs.reportImg.toImg().then((base64)=>{
								var arr = base64.split(',')
								var mime = arr[0].match(/:(.*?);/)[1]
								var bstr = atob(arr[1]) 
								var nn = bstr.length 
								var u8arr = new Uint8Array(nn);
								while(nn--){
									u8arr[nn] = bstr.charCodeAt(nn);
								}
								var obj = new Blob([u8arr], {type:mime});
								_this.$refs.reportImg.toImg2().then((base642)=>{
									var arr = base642.split(',')
									var mime = arr[0].match(/:(.*?);/)[1]
									var bstr = atob(arr[1]) 
									var nn = bstr.length 
									var u8arr = new Uint8Array(nn);
									while(nn--){
										u8arr[nn] = bstr.charCodeAt(nn);
									}
									var obj2  = new Blob([u8arr], {type:mime});
									var formData = new FormData()
									formData.append('file1', obj)  
									formData.append('file2', obj2)  
									_this.$api.appAssessSummaryUpload(res.data.result.id,formData).then((ares)=>{
										if(ares.data.returnCode == 200){
											uni.navigateTo({
												url:'/pages/currentSummary/list'
											})
										}
									})
								})
							})
						},500)
					}else{
						uni.showToast({
							title:res.data.message,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/quickly/healthStatus/common.scss';
	.selectMember {
		display: flex;
		.member {
			flex: 1;
			display: flex;
			view {
				width: 200upx;
			}
			view:nth-of-type(2) {
				flex: 1;
				margin-left: 0 !important;
			}
		}
	}
	.hasData,.metadata {
		width: 702upx;
		margin: 130upx 24upx;
		padding-top: 24upx;
		.summary {
			width: 702upx;
			background: #FFFFFF;
			border-radius: 12upx;
			position: relative;
			padding: 0 28upx 0 42upx;
			box-sizing: border-box;
			margin-bottom: 24upx;
			.topLeft {
				position: absolute;
				left: 0;
				width: 278upx;
				height: 56upx;
				margin-bottom: 32upx;
			}
			.top {
				padding-top: 86upx;
				display: flex;
				position: relative;		
				border-bottom: 1upx solid #cccccc;
				padding-bottom: 24upx;
				image {
					position: absolute;
					left: -48upx;
					top: 61%;
					width: 32upx;
					height: 30upx;
				}
				view {
					flex: 1;
				}
				view:last-child{
					text-align: right;
				}
			}
			.bottom {
				display: flex;
				padding: 25upx 0 35upx;
				view:nth-child(1) {
					flex: 1;
					color: #333333;
					font-size: 40upx;
					font-weight: bold;
				}
				view:last-child {
					width: 146upx;
					height: 50upx;
					background: #6AB7FF;
					border-radius: 25upx;
					color: #FFFFFF;
					font-size: 30upx;
					line-height: 50upx;
					text-align: center;
				}
			}
		}
	}
	
	.noData {
		width: 702upx;
		height: 96upx;
		background: #FFFFFF;
		border-radius: 12upx;
		position: relative;
		color: #677092;
		font-size: 32upx;
		text-align: right;
		padding-right: 34upx;
		box-sizing: border-box;
		line-height: 96upx;
		margin-bottom: 24upx;
		.topLeft {
			position: absolute;
			left: 0;
			top: 0;
			width: 278upx;
			height: 56upx;
			margin-bottom: 32upx;
		}
	}
	
	.metadata {
		.case {
			width: 702upx;
			height: 96upx;
			background: #FFFFFF;
			border-radius: 12upx;
			display: flex;
			align-items: center;
			padding: 0 34upx 0 0;
			box-sizing: border-box;
			margin-bottom: 24upx;
			image {
				width: 6upx;
				height: 20upx;
			}
			.title {
				flex: 1;
				color: #05C6AD;
				font-size: 34upx;
				font-weight: bold;
				padding-left: 38upx;
			}
			view:last-child {
				flex: 1;
				text-align: right;
				color: #677092;
				font-size: 32upx;
			}
		}
	}
</style>
